const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
const jsonData = require("../jsondata.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    scid: '',
    customerid: '',
    leadName: '',
	addOrMod:'add',
    //屈光检查选项集
    isMyopiaIndex: -1,
    isWearGlassesIndex:-1,
    isOptometryGlassesIndex: -1,
	haveOrNot:jsonData.haveOrNot,
	isOptometryGlassesArray:jsonData.isOptometryGlasses,
	isWearGlassesArray:jsonData.isWearGlasses,
  schoolId:'',
    /**屈光检查 */
    //是否近视
    isMyopia: { new_scid: null, new_categoryid: 'LB0005', new_itemid: 'XM0041', new_contentid: 'NR0136', new_result: 'SC0000000029', new_doctorid: null, ownerid: null },
    //近视年限
    myopiaYear: { new_scid: null, new_categoryid: 'LB0005', new_itemid: 'XM0041', new_contentid: 'NR0138', new_result: '', new_doctorid: null, ownerid: null },
    //佩戴眼镜
    isWearGlasses: { new_scid: null, new_categoryid: 'LB0005', new_itemid: 'XM0042', new_contentid: 'NR0131', new_result: 'SC0000000029', new_doctorid: null, ownerid: null },
    //是否医学验光眼镜
    isOptometryGlasses: { new_scid: null, new_categoryid: 'LB0005', new_itemid: 'XM0043', new_contentid: 'NR0134', new_result: 'SC0000000051', new_doctorid: null, ownerid: null },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var options = options;
    if (options) {
    	this.setData({
    		leadName: options.leadname,
    		customerid: options.sccustid,
    		scid: options.scid,
    		categoryid: options.categoryid
    	})
    	this.getRefractiveexaminationResults(options.scid, options.sccustid, options.categoryid);
      if(options.schoolId){
        this.setData({schoolId:options.schoolId});
      }
    }
  },
  //屈光检查提交
  refractiveExaminationSubmit: function () {
    var createResult = this.getFractiveExamination();
   var that = this;
   commonutil.createScreenResults(createResult,function(res){
	   var idArray = res.result.split('|');
	   for(var i = 0;i < idArray.length;i++){
	   	createResult[i].new_sc_resultId = idArray[i];
	   }
	   that.setData({
	   	addOrMod: "mod",
	   	isMyopia:	createResult[0],
	   	myopiaYear:	createResult[1],
	   	isWearGlasses:	createResult[2],
	   	isOptometryGlasses:	createResult[3],
	   });
   });
  },
  //更新
  refractiveExaminationUpdate:function(){
  	var updateResult = this.getFractiveExamination();
  	commonutil.updateScreenResults(updateResult);
  },
  //查看报告
  enterDetail: async function(e) {
  	var scid = this.data.scid;
	var leadid = this.data.customerid;
  	var lead = {};
  	var data = await commonutil.getLeadInfo(leadid);
  	lead.leadid = data.leadId;
  	lead.customername = data.lastName;
  	lead.age = data.new_age;
  	lead.sex = data.new_gender;
  	lead.iphoneno1 = data.new_phone;
  	lead.sfz = data.new_citizenid;
  	lead.createdon = data.modifiedOn;
    var url = "/pages/print/letter/letter?scid=" + scid;
    if (this.data.schoolId)
      url += "&schoolId=" + this.data.schoolId;
  	wx.navigateTo({
  		url: url,
  		events: {
  			// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
  			acceptDataFromOpenedPage: function(data) {
  				console.log(data)
  			},
  			someEvent: function(data) {
  				console.log(data)
  			}
  		},
  		success: function(res) {
  			// 通过eventChannel向被打开页面传送数据
  			res.eventChannel.emit('acceptDataFromOpenerPage', {
  				data: lead
  			})
  		}
  	})
  },
  //获取结果集
  getRefractiveexaminationResults: async function(scid, customerid, categoryid) {
  	wx.showLoading({
  		title:'加载中...',
  		mask:true,
  	})
  	var success = await screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
  	var reusltArray = success.data
  	if(reusltArray.length==0){
  		wx.hideLoading();
  		return ;
  	}
  	this.setData({
  		addOrMod:'mod'
  	})
  	for (let index in reusltArray) {
  		var element = reusltArray[index];
  		if (element.new_contentid === 'NR0136') {
  			this.setData({
  				isMyopia: element
  			})
  		} else if (element.new_contentid === 'NR0138') {
  			this.setData({
  				myopiaYear: element
  			})
  		} else if (element.new_contentid === 'NR0131') {
  			this.setData({
  				isWearGlasses: element
  			})
  		} else if (element.new_contentid === 'NR0134') {
  			this.setData({
  				isOptometryGlasses: element
  			})
  		} 
  	}
  	wx.hideLoading();
  },
  /**屈光检查选项集 、文本赋值*/
  onIsMyopiaChange: function (e) {
	var result = 'isMyopia.new_result';
    this.setData({
	[result]:'',
      isMyopiaIndex: e.detail.index
    })
  },
  onIsWearGlassesChange: function (e) {
    this.setData({
      isWearGlassesIndex: e.detail.index
    })
  },
  onIsOptometryGlassesChange: function (e) {
    this.setData({
      isOptometryGlassesIndex: e.detail.index
    })
  },
  onMyopiaYearChange: function (e) {
    var myopiaYearResult = 'myopiaYear.new_result';
    this.setData({
      [myopiaYearResult]: e.detail,
    })
  },
  //屈光检查
  getFractiveExamination: function () {
    var createResult = [];
    var isMyopia = this.data.isMyopia;
    var myopiaYear = this.data.myopiaYear;
    var isWearGlasses =this.data.isWearGlasses;
    var isOptometryGlasses =this.data.isOptometryGlasses;
    var scid = this.data.scid;
    var customerid = this.data.customerid;
    //是否近视
    var isMyopiaIndex = this.data.isMyopiaIndex;
	var haveOrNot = this.data.haveOrNot;
    if (isMyopiaIndex >-1) {
      isMyopia.new_result = haveOrNot[isMyopiaIndex];
    }
    //佩戴眼镜
    var isWearGlassesIndex = this.data.isWearGlassesIndex;
	var isWearGlassesArray = this.data.isWearGlassesArray; 
    if (isWearGlassesIndex >-1) {
      isWearGlasses.new_result =  isWearGlassesArray[isWearGlassesIndex];
    } 
    //是否医学验光眼镜
    var isOptometryGlassesIndex = this.data.isOptometryGlassesIndex;
	var isOptometryGlassesArray = this.data.isOptometryGlassesArray;
    if (isOptometryGlassesIndex >-1) {
      isOptometryGlasses.new_result = isOptometryGlassesArray[isOptometryGlassesIndex];
    }

    createResult.push(isMyopia);
	if(isMyopia.new_result=='SC0000000008'&&myopiaYear.new_result!=''){
		createResult.push(myopiaYear);
	}
    createResult.push(isWearGlasses);
    createResult.push(isOptometryGlasses);
     //给各结果的筛查id/联系人id赋值
     createResult.forEach(element => {
      element.new_scid=scid;
      element.new_customerid = customerid;
    });
    return createResult;
  },


})